package com.demo.common.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.IOException;

@Slf4j
@Configuration
public class FilterAutoConfiguration {

    @Bean
    public FilterRegistrationBean<Filter> testFilter() {
        FilterRegistrationBean<Filter> bean = new FilterRegistrationBean<>(new TestFilter());
        log.info("bean [{}]", bean);
        return bean;
    }

    /**
     * 测试filter,啥也不干，演示用
     */
    private class TestFilter implements Filter{

        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            log.info("request do filter,this is a test filter,do nothing");
            chain.doFilter(request,response);
        }
    }

}
